package com.example.carefree_server.controller.c_managerController;

import com.example.carefree_server.entity.Department;
import com.example.carefree_server.entity.Result;
import com.example.carefree_server.service.s_managerService.departmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/departments")
public class departmentController {
    @Autowired
    private departmentService depService;

//    获取所有部门信息
    @GetMapping("/findAll")
    public List<Department> findAll(){
        return depService.getAllDepartments();
    }

//    查找详情部门信息
    @GetMapping("/findById")
    public Result findById(String did){
        Department department=depService.findDepartmentById(did);
//        查找信息为空，既是系统不存在该部门
        if(department==null){
            return Result.error("该部门不存在！");
        }else {
            return Result.success(department);
        }
    }

//    添加部门信息
    @PostMapping("/insertDepartment")
    public Result insertDepartment(@RequestBody Department department){
        String id=department.getDid();
//        只有插入的部门不存在系统（返回值为空）中，才可以执行操作
        if (depService.findDepartmentById(id)==null){
            depService.insertDepartment(department);
            return  Result.addSuccess();
        }else{
            return Result.error("添加失败！该部门已存在！");
        }
    }

//    更新部门信息
    @PutMapping("/updateDepartment")
    public Result updateDepartment(@RequestBody Department department){
        String id=department.getDid();
//        只有更新部门存在系统中（返回值不为空），才可以执行操作
        if (depService.findDepartmentById(id)!=null){
            depService.updateDepartment(department);
            return  Result.UpdateSuccess();
        }else{
            return Result.error("无法更新，该部门不存在");
        }

    }

//    删除部门信息
    @DeleteMapping("/{did}")
    public Result deleteDepartment(@PathVariable String did){
//        只有所删除部门存在系统中（返回值不为空），才可以执行操作
        if (depService.findDepartmentById(did)!=null){
            depService.deleteDepartment(did);
            return  Result.deleteSuccess();
        }else{
            return Result.error("该部门不存在！无法删除");
        }


    }


}
